<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class AddressBookController extends Controller
{

    protected $userAttr = [
        'name'      => '姓名',
        'telephone' => '手机号码',
        'email'     => '邮箱地址',
        'company'   => '工作单位',
    ];

    /**
     * 列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $build = app(User::class);
        if($request->has('name')){
            $build = $build->where('name','like',"%$request->name%");
        }
        if($request->has('telephone')){
            $build = $build->where('telephone','like',"%$request->telephone%");
        }
        $users = $build->orderby('id','desc')->paginate(10);
        return view('welcome', compact('users'));
    }

    /**
     * 创建页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create(Request $request)
    {
        return view('create');
    }

    /**
     * 创建操作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function createPost(Request $request)
    {
        $this->validate($request, [
            'name'      => 'required',
            'telephone' => 'required|regex:/^\d+$/',
        ], [], $this->userAttr);

        app(User::class)->insert([
            'name'      => $request->name,
            'telephone' => $request->telephone,
            'company'   => $request->get('company', ''),
            'email'     => $request->get('email', ''),
        ]);
        return redirect(route('user.list'))->with(['successMsg' => '新增成功！']);
    }

    /**
     * 修改页面
     * @param Request $request
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function show(Request $request, $id)
    {
        $user = app(User::class)->find($id);
        if ($user) {
            return view('updateShow', compact('user'));
        }
        return back()->withErrors('该联系人不存在！');

    }

    /**
     * 修改操作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request)
    {
        $this->validate($request, [
            'id'        => 'exists:t_users,id',
            'name'      => 'required',
            'telephone' => [
                'required',
                'regex:/^\d+$/'
            ],
        ], [], $this->userAttr);
        $exists = app(User::class)->where([
            'telephone' => $request->telephone
        ])->where('id', '<>', $request->id)->exists();
        if ($exists) {
            return back()->withErrors('手机号码不能重复');
        }

        app(User::class)->where([
            'id' => $request->id
        ])->update([
            'name'      => $request->name,
            'telephone' => $request->telephone,
            'company'   => $request->get('company', ''),
            'email'     => $request->get('email', ''),
        ]);
        return redirect(route('user.list'))->with(['successMsg' => '更新成功！']);
    }

    /**
     * 删除操作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete(Request $request)
    {
        $user = app(User::class)->find($request->id);
        if (!$user) {
            return back()->withErrors('该联系人不存在！');
        }
        $user->delete();
        return redirect(route('user.list'))->with(['successMsg' => '删除成功！']);

    }

}
